import { routes } from '@/router'
import config from '@/config'

export const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export function formatDate(date = new Date(), enterSymbol = '-') {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join(enterSymbol)
}

export function formatTime(date, enterSymbol = '-') {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join(enterSymbol) + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
 * 节流函数
 * @param {Function} func
 * @param {Number} wait
 * @param {Object} options
 */
export function throttle(func, wait, options = {}) {
  let timeout, context, args, result
  let previous = 0
  let later = function() {
    previous = options.leading === false ? 0 : Date.now() || new Date().getTime()
    timeout = null
    result = func.apply(context, args)
    if (!timeout) context = args = null
  }
  let throttled = function() {
    let now = Date.now() || new Date().getTime
    if (!previous && options.leading === false) previous = now
    let remaining = wait - (now - previous)
    context = this
    args = arguments
    if (remaining <= 0 || remaining > wait) {
      if (timeout) {
        clearTimeout(timeout)
        timeout = null
      }
      previous = now
      result = func.apply(context, args)
      if (!timeout) context = args = null
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining)
    }
    return result
  }

  throttled.cancel = function() {
    clearTimeout(timeout)
    previous = 0
    timeout = context = args = null
  }

  return throttled
}

/**
 * 处理权限路由
 */
export function handleMenuListRoutes(menuList = []) {
  return menuList.reduce((res, menu) => {
    routes.map(route => {
      if (route.meta.title === menu.title) {
        let children = []
        route.children.map(routeItem => {
          if (menu.children && Array.isArray(menu.children) && menu.children.length > 0) {
            menu.children.map(menuItem => {
              if (menuItem.title === routeItem.meta.title) {
                let { id, parentid } = menuItem
                children.push({ ...routeItem, meta: { ...routeItem.meta }, id, parentid })
              }
            })
          } else {
            if (menu.title === routeItem.meta.title) {
              let { id, parentid } = menu
              children.push({ ...routeItem, meta: { ...routeItem.meta }, id, parentid })
            }
          }
        })
        let { id, parentid } = menu
        res.push({ ...route, meta: { ...route.meta }, id, parentid, children })
      }
    })
    return res
  }, [])
}

/**
 * 查找左侧菜单第一个页面name
 */
export function handleFirstRoute(menuList = []) {
  if (menuList && Array.isArray(menuList) && menuList.length > 0) {
    let { children } = menuList[0]
    if (children && Array.isArray(children) && children.length > 0) {
      return children[0].name
    } else {
      return menuList[0].name
    }
  } else {
    return config.routeName
  }
}

/**
 * 平铺所有菜单/权限
 */
export function handleFlat(arr) {
  return arr.reduce((res, item) => {
    let arr = []
    if (item.children && Array.isArray(item.children) && item.children.length > 0) {
      arr = handleFlat(item.children)
    }
    return [...res, item, ...arr]
  }, [])
}
